//
//  ___FILENAME___
//  ___PACKAGENAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright (c) ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.
//

#import "GLViewController.h"
#import "ConstantsAndMacros.h"
#import "OpenGLCommon.h"

@implementation GLViewController

- (void)loadView
{
    GLView *viewGL = [[GLView alloc] init];
    viewGL.delegate = self;
    self.view = viewGL;
    [viewGL release];
	
	[viewGL startAnimation];
}


- (void)drawCubes {
	static GLfloat rot = 0.0;
	
	static const Vertex3D vertices[]= {
        {0, -0.525731, 0.850651},             // vertices[0]
        {0.850651, 0, 0.525731},              // vertices[1]
        {0.850651, 0, -0.525731},             // vertices[2]
        {-0.850651, 0, -0.525731},            // vertices[3]
        {-0.850651, 0, 0.525731},             // vertices[4]
        {-0.525731, 0.850651, 0},             // vertices[5]
        {0.525731, 0.850651, 0},              // vertices[6]
        {0.525731, -0.850651, 0},             // vertices[7]
        {-0.525731, -0.850651, 0},            // vertices[8]
        {0, -0.525731, -0.850651},            // vertices[9]
        {0, 0.525731, -0.850651},             // vertices[10]
        {0, 0.525731, 0.850651}               // vertices[11]
    };
	
	static const Color3D colors[] = {
		{1.0, 0.0, 0.0, 1.0},
		{1.0, 0.5, 0.0, 1.0},
		{1.0, 1.0, 0.0, 1.0},
		{0.5, 1.0, 0.0, 1.0},
		{0.0, 1.0, 0.0, 1.0},
		{0.0, 1.0, 0.5, 1.0},
		{0.0, 1.0, 1.0, 1.0},
		{0.0, 0.5, 1.0, 1.0},
		{0.0, 0.0, 1.0, 1.0},
		{0.5, 0.0, 1.0, 1.0},
		{1.0, 0.0, 1.0, 1.0},
		{1.0, 0.0, 0.5, 1.0}
    };
	
	static const GLubyte icosahedronFaces[] = {
        1, 2, 6, //第一个点:{0.850651, 0, 0.525731} 第二个点:{0.850651, 0, -0.525731} 第6个点:{0.525731, 0.850651, 0} 构成的三角形
        1, 7, 2,
        3, 4, 5,
        4, 3, 8,
        6, 5, 11,
        5, 6, 10,
        9, 10, 2,
        10, 9, 3,
        7, 8, 9,
        8, 7, 0,
        11, 0, 1,
        0, 11, 4,
        6, 2, 10,
        1, 6, 11,
        3, 5, 10,
        5, 4, 11,
        2, 7, 9,
        7, 1, 0,
        3, 9, 8,
        4, 8, 0,
    };
	
	glLoadIdentity();
    glClearColor(0.7, 0.7, 0.7, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glColorPointer(4, GL_FLOAT, 0, colors);
	
	for (int i = 1; i <= 30; i++)//画30个20面体
    {
        glLoadIdentity();
        glTranslatef(0.0f,-1.5,-3.0f * (GLfloat)i);
        glRotatef(rot, 1.0, 1.0, 1.0);
        glDrawElements(GL_TRIANGLES, 60, GL_UNSIGNED_BYTE, icosahedronFaces);
    }
	
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
	
    static NSTimeInterval lastDrawTime;
    if (lastDrawTime)
    {
        NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
        rot+=50 * timeSinceLastDraw;
    }
	
    lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
}

#pragma mark - GLViewDelegate

- (void)drawView:(UIView *)theView
{
	[self drawCubes];
}

#pragma mark - Setup View

//正交视图
- (void)setupOrthoView:(UIView *)view {
	CGRect rect = view.bounds;
	
	//设置正交空间
	glOrthof(-1.0, //left
			 1.0, //right
			 -1.0/(rect.size.width / rect.size.height),//bottom
			 1.0/(rect.size.width / rect.size.height) ,//top
			 0.01,//near
			 1000.0); //far
	
	glViewport(0, 0, rect.size.width, rect.size.height); //视口大小
}

//透视视图/远景视图
- (void)setupPerspective:(UIView *)view {
	
	const GLfloat zNear = 0.01, zFar = 1000.0, fieldOfView = 45.0;//视野设为45度
	GLfloat size;
	size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
	CGRect rect = view.bounds;
	
	//设置锥形空间
	glFrustumf(-size,
			   size,
			   -size / (rect.size.width / rect.size.height),
			   size /  (rect.size.width / rect.size.height),
			   zNear,
			   zFar);
	
	glViewport(0, 0, rect.size.width, rect.size.height);
}

- (void)setupView:(GLView*)view
{
	
	glEnable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	
	//[self setupOrthoView:view]; //由于是正交视图，后面的20面体都会被挡住
	[self setupPerspective:view]; //远近视图，后面的20面体会越来越小
	
	glMatrixMode(GL_MODELVIEW);
	
	glLoadIdentity();
}


@end
